// cartItem
// {
//   id: 1,
//   url: 'https://pic.maizuo.com/usr/movie/df44cf38656235812fe38494e18d17ed.jpg?x-oss-process=image/resize,m_fixed,h_0,w_300',
//   title: '江湖渣',
// }
export default {
  addToCart(state, cartItem) {
    const isInCart = state.cart.some(item => item.id === cartItem.id);
    if (isInCart) {
      state.cart = state.cart.map((item) => {
        const newItem = Object.assign({}, item);
        if (newItem.id === cartItem.id) {
          newItem.count += 1;
        }
        return newItem;
      });
    } else {
      state.cart.push({
        count: 1,
        ...cartItem,
      });
    }
    window.localStorage.setItem('ca-cart', JSON.stringify(state.cart));
  },
  changeCartCountByIdAndType(state, payload) {
    state.cart = state.cart.map((item) => {
      const newItem = Object.assign({}, item);
      if (newItem.id === payload.id) {
        if (payload.type === 'add') {
          newItem.count += 1;
        } else {
          newItem.count -= 1;
        }
      }
      return newItem;
    });
    window.localStorage.setItem('ca-cart', JSON.stringify(state.cart));
  },
};
